package com.open.party.cloud.view.home.eJia

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import cn.android.x.plugins.webView.WebViewForFilePreviewActivity
import com.open.party.cloud.R
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.FileEntity
import com.open.party.cloud.model.PartyLifeBean
import com.open.party.cloud.view.comm.view.MeetFileListView
import com.open.party.cloud.viewModel.PartyViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.views.fileView.FileNetListView
import com.sinothk.android.views.fileView.bean.FileShowListEntity
import kotlinx.android.synthetic.main.party_life_detail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class PartyLifeDetailActivity : AppEventBusTitleBaseActivity() {
    var viewModel: PartyViewModel? = null
    var id = ""

    override fun getLayoutResId(): Int = R.layout.party_life_detail

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("活动详情")

        id = intent.getStringExtra("id") as String

        viewModel = PartyViewModel()
        showLoadingDialog("正在加载")
        viewModel!!.getPartyLifeDetailById(id)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<PartyLifeBean>) {
        if (result.eventType != "getPartyLifeDetailById") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                showDetail(result.data)
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showDetail(freePhotosBean: PartyLifeBean) {

        titleTv.text = XUtils.string().getNotNullValue(freePhotosBean.name, "")
        orgNameTv.text = XUtils.string().getNotNullValue(freePhotosBean.orgName, "")
        leaderTv.text = XUtils.string().getNotNullValue(freePhotosBean.dutyPerson, "")

        startTimeTv.text = XUtils.date().getDateStrByDate(freePhotosBean.startTime, "yyyy-MM-dd HH:mm")
        endTimeTv.text = XUtils.date().getDateStrByDate(freePhotosBean.endTime, "yyyy-MM-dd HH:mm")

        addressTv.text = XUtils.string().getNotNullValue(freePhotosBean.addr, "")
        contentTv.text = XUtils.string().getNotNullValue(freePhotosBean.content, "")

        showFileView(freePhotosBean.showList)
    }

    // 显示图片
    private fun showFileView(fileList: ArrayList<FileEntity>?) {
        if (fileList == null || fileList.size == 0) {
            fileView.visibility = View.GONE
        } else {
            fileView.visibility = View.VISIBLE

            val showFileList = ArrayList<FileShowListEntity>()
            for (fileBean in fileList){
                val fBean = FileShowListEntity()
                fBean.fileName = fileBean.fileName
                fBean.fileSize = "0"
                fBean.filePath = fileBean.filePath

                showFileList.add(fBean)
            }
            fileListView.setData(showFileList) { entity ->
                WebViewForFilePreviewActivity.start(this@PartyLifeDetailActivity, entity.fileName, ServerConfig.getFilePreviewUrl(entity.filePath))
            }
        }
    }
}